library(sf)
library(tidyverse)
library(ragg)
# this code was written before the release of sf
sf_use_s2(FALSE)

######
# ML Predictions
######
pred_ASM_robust_files <- list.files("Data/ML Prediction/Predictions",
                                    "prediction_model_ASM_\\d+_individual_hp\\.dill\\.csv")

pred_ASM_robust_seeds <- pred_ASM_robust_files %>% 
  str_split("_|\\.") %>% 
  sapply(`[`, 5) %>% 
  as.numeric()

voted_asm <- 
  lapply(paste0("Data/ML Prediction/Predictions/", pred_ASM_robust_files), read_csv) %>% 
  bind_rows()

#####
# Grids
#####

# this section is needed to subset to the GIDs within the predictive region 
asm_sites_cod <- st_read("Data/IPIS/cod_mines_curated_all_opendata_p_ipis.shp")
asm_sites_tza <- st_read("Data/IPIS/tza_mines_curated_all_opendata_p_ipis.shp")

asm_sites_burkina <- st_read("Data/Burkina ASM/Sites_Potentiels.shp")

grids <- st_read("Data/ML Prediction/Predictors/gridded_covariates_snl.gpkg")

asm_vote_share_grids <- 
  grids %>%
  select(gid) %>% 
  left_join(voted_asm %>% 
              group_by(gid) %>% 
              summarise("Mean of Predicted Probability" = mean(ASM_prob), 
                        "SD of Predicted Probability" = sd(ASM_prob)) %>% 
              ungroup() %>% 
              pivot_longer(-c(gid), names_to = "variable", values_to = "value"))
#####
# Figure
#####

agg_png("./Figures/Figure-A-10-pedicted-probability-map.png", width = 7, height = 4, units = "in", res = 300)
  print(
  asm_vote_share_grids %>%
    filter(!is.na(value)) %>%
    ggplot()+ 
      geom_sf(aes(fill = value), size = 0, color = NA) + 
      facet_grid(cols = vars(variable)) + 
      scale_fill_viridis_c(option = "D", begin = .5, end = 0) + 
      labs(title = "Predicted Probability of ASM")
  )
dev.off()
